﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Dude.Data.Meta;

namespace Dude.Data.Postgres
{
    public class PostgresMetadataGenerator : DbMetadataGenerator
    {
        public override DbCommand GetShowTablesCommand()
        {
            DbCommand cmd = ProviderFactory.CreateCommand();
            cmd.CommandText = "SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r'";
            cmd.Connection = Connection;
            cmd.Transaction = Transaction;
            return cmd;
        }

        public override DbCommand GetShowFieldsCommand()
        {
            DbProviderFactory dbfactory = ProviderFactory;
            DbCommand cmd = dbfactory.CreateCommand();
            //cmd.CommandText = "SELECT a.attname FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = @tablename AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ";
            cmd.CommandText = "select column_name,data_type,character_maximum_length,numeric_precision,numeric_precision_radix from information_schema.columns where table_name = @tablename order by ordinal_position;";
            cmd.Connection = Connection;
            cmd.Transaction = Transaction;
            DbParameter param = dbfactory.CreateParameter();
            param.ParameterName = "@tablename";
            cmd.Parameters.Add(param);
            return cmd;
        }

        public override DataModel UpdateDataModel(DataModel original, MetadataUpdateMode updateMode)
        {
            return original;
        }
    }
}
